import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { loginService } from '@/api/login'
import { useCartStore } from '@/stores'

type loginParams = {
  account: string
  password: string
}
export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref({})
    const cartStore = useCartStore()
    const getUserInfo = async ({ account, password }: loginParams) => {
      const res = await loginService({ account, password })
      userInfo.value = res.data.result
    }
    const clearUserInfo = () => {
      userInfo.value = {}
      cartStore.clearCartList()
    }
    const isLogin = computed(() => userInfo.value?.token)
    return { userInfo, getUserInfo, clearUserInfo, isLogin }
  },
  {
    persist: true
  }
)
